// MegaDialog.cpp : implementation file
//

#include "stdafx.h"
#include "MegaDialog.h"
#include "MegaBitmap.h"
#include "MegaDrawer.h"
#include "resource.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMegaDialog dialog
#define ID_BTN_MIN                      1054
#define ID_BTN_MAX                      1055
#define ID_BTN_CLOSE                    1056

CMegaDialog::CMegaDialog(UINT id, CWnd* pParent, UINT uiSkinSingle, UINT uiSkinDouble)
	: CDialog(id, pParent)
{
	m_pSkinSingle = new CMegaBitmap();
	m_pSkinDouble = new CMegaBitmap();
	m_pSkinSingle->LoadBitmap(uiSkinSingle);
	m_pSkinDouble->LoadBitmap(uiSkinDouble);

	m_nSize = 1;
	m_pBackgroundBmp = m_pSkinSingle;

	m_hrgnBase = m_pSkinSingle->CreateRegion(RGB(255, 0, 0));
	m_hrgnBaseDouble = m_pSkinDouble->CreateRegion(RGB(255, 0, 0));
}

CMegaDialog::~CMegaDialog()
{
	if(m_pSkinSingle)
		delete m_pSkinSingle;
	if(m_pSkinDouble)
		delete m_pSkinDouble;
}

BOOL CMegaDialog::OnInitDialog()
{
	// create min/max/close buttons
	DWORD dwButtonStyle = WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON;

	m_CloseBtn.Create("", dwButtonStyle, CRect(), this, ID_BTN_CLOSE);
	m_MaxBtn.Create("", dwButtonStyle|WS_DISABLED, CRect(), this, ID_BTN_MAX);
	m_MinBtn.Create("", dwButtonStyle, CRect(), this, ID_BTN_MIN);

	m_MinBtn.SetBitmaps(IDB_MIN_OUT, IDB_MIN_DOWN, IDB_MIN_OVER);
	m_MaxBtn.SetBitmaps(IDB_MAX_OUT, IDB_MAX_DOWN, IDB_MAX_OVER);
	m_CloseBtn.SetBitmaps(IDB_CLOSE_OUT, IDB_CLOSE_DOWN, IDB_CLOSE_OVER);

	m_MaxBtn.SetBackgroundBmp(m_pBackgroundBmp);
	m_MinBtn.SetBackgroundBmp(m_pBackgroundBmp);
	m_CloseBtn.SetBackgroundBmp(m_pBackgroundBmp);

	return TRUE;
}

void CMegaDialog::AdjustRegion()
{
	CSize szBase = m_pBackgroundBmp->GetBitmapDimension();

	int nDrawerHeight = 0;
           int i;
	for(i = 0; i < m_Drawers.size(); i++)
	{
		CMegaDrawer * pDrawer = m_Drawers[i];
		CSize sz = pDrawer->GetCurrentSkin()->GetBitmapDimension();
		if(sz.cy > nDrawerHeight)
			nDrawerHeight = sz.cy;
	}

	// enlarge main window
	// check if right-bottom edge is outside screen
	CRect rcMainWnd;
	GetWindowRect(&rcMainWnd);
	RECT rcMonitor;
	::SystemParametersInfo(SPI_GETWORKAREA, 0, &rcMonitor, 0);

	int nScreenWidth	= rcMonitor.right-rcMonitor.left; //::GetSystemMetrics(SM_CXSCREEN);
	int nScreenHeight	= rcMonitor.bottom-rcMonitor.top; //::GetSystemMetrics(SM_CYSCREEN);

	if(rcMainWnd.left + szBase.cx >= nScreenWidth)
		rcMainWnd.left = nScreenWidth - szBase.cx;

	if(rcMainWnd.top + szBase.cy + nDrawerHeight >= nScreenHeight)
		rcMainWnd.top = nScreenHeight - szBase.cy - nDrawerHeight;

	SetWindowPos(NULL, rcMainWnd.left, rcMainWnd.top, szBase.cx, szBase.cy+nDrawerHeight, SWP_NOZORDER);
	// reposition drawer
	for(i = 0; i < m_Drawers.size(); i++)
	{
		m_Drawers[i]->SetWindowPos(NULL, 0, szBase.cy, 0, 0, SWP_NOZORDER|SWP_NOSIZE);
	}

	// set window region, according to drawers
	HRGN hrgnBase = ::CreateRectRgn(0, 0, 0, 0);
	if(m_nSize == 1)
		::CombineRgn(hrgnBase, m_hrgnBase, m_hrgnBase, RGN_COPY);
	else
		::CombineRgn(hrgnBase, m_hrgnBaseDouble, m_hrgnBaseDouble, RGN_COPY);

	// get drawer's regions
	for(i = 0; i < m_Drawers.size(); i++)
	{
		CMegaDrawer * pDrawer = m_Drawers[i];
		HRGN hrgn = ::CreateRectRgn(0, 0, 0, 0);
		::GetWindowRgn(m_Drawers[i]->GetSafeHwnd(), hrgn);
		::OffsetRgn(hrgn, 0, szBase.cy);
		::CombineRgn(hrgnBase, hrgnBase, hrgn, RGN_OR);
	}

	// force to redraw background on edges of screen
	CRect rcWnd;
	GetClientRect(&rcWnd);
	CRect rc1(0, 0, m_pSkinSingle->GetBitmapDimension().cx, 50);
	CRect rc2(0, 0, 3, rcWnd.Height());
	InvalidateRect(&rc1);
	InvalidateRect(&rc2);

	// set total window region
	::SetWindowRgn(GetSafeHwnd(), hrgnBase, TRUE);
}

BEGIN_MESSAGE_MAP(CMegaDialog, CDialog)
	//{{AFX_MSG_MAP(CMegaDialog)
	ON_WM_SYSCOMMAND()
	ON_BN_CLICKED(ID_BTN_MIN, OnMinimize)
	ON_BN_CLICKED(ID_BTN_MAX, OnMaximize)
	ON_BN_CLICKED(ID_BTN_CLOSE, OnClose)
	ON_WM_NCHITTEST()
	ON_MESSAGE(WM_DRAWER_ACTIVATION, OnDrawerActivation)
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMegaDialog message handlers
void CMegaDialog::OnSysCommand(UINT nID, LPARAM lParam)
{
	if(SC_CLOSE == nID)
		EndDialog(IDOK);
	else
		CDialog::OnSysCommand(nID, lParam);
}

void CMegaDialog::OnOK()
{
// do nothing, just dont want dialog to finish
}

void CMegaDialog::OnCancel()
{
// do nothing, just dont want dialog to finish
}

void CMegaDialog::OnMinimize()
{
	ShowWindow(SW_MINIMIZE);
}

void CMegaDialog::OnMaximize()
{
	ShowWindow(SW_MAXIMIZE);
}

void CMegaDialog::OnClose()
{
	CDialog::OnOK();
}

LRESULT CMegaDialog::OnNcHitTest(CPoint point)
{
	ScreenToClient(&point);
	if(point.y <= 28)
		return HTCAPTION;
	else
		return HTCLIENT;
}

LRESULT CMegaDialog::OnDrawerActivation(WPARAM wParam, LPARAM lParam)
{
	CMegaDrawer * pSender = (CMegaDrawer*) wParam;

	if(!pSender->IsActive())
	{
		for(int i = 0; i < m_Drawers.size(); i++)
			m_Drawers[i]->Activate(true);
	}
	else
	{
		if(pSender->IsOnTop())
		{
			for(int i = 0; i < m_Drawers.size(); i++)
				m_Drawers[i]->Activate(false);
		}
	}

	for(int i = 0; i < m_Drawers.size(); i++)
	{
		CMegaDrawer * pDrawer = m_Drawers[i];
		if(pDrawer != pSender)
			pDrawer->MoveToTop(false);
	}
	pSender->MoveToTop(true);

	AdjustRegion();

	return 0;
}

BOOL CMegaDialog::OnEraseBkgnd(CDC* pDC)
{
	CRect rc;
	GetClientRect(&rc);

	// background
	m_pBackgroundBmp->Draw(pDC, 0, 0);

	return 1;
}
